//
//  HomeBallHeaderView.m
//  Btpin
//
//  Created by Vic on 2019/12/14.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "HomeBallHeaderView.h"
#import "DBSphereView.h"
#import "HomeLinkItemView.h"

#define BtnTag (7100)

@interface HomeBallHeaderView ()
{
    DBSphereView *sphereView;
    NSMutableArray *itemDataArray;
    
    UILabel *jobLbl;
}
@end

@implementation HomeBallHeaderView

- (instancetype)initWithFrame:(CGRect)frame dataArray:(NSArray *)dataArray target:(id)target idType:(TabBarType)idType {
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        itemDataArray = [NSMutableArray arrayWithCapacity:0];
        [itemDataArray addObjectsFromArray:dataArray];
        [self initUIWithIdType:idType];
        self.delegate = target;
    }
    return self;
}
- (void)initUIWithIdType:(TabBarType)idType {
    self.backgroundColor = COLOR_OF_WHITE;
    UIImageView *imgView = [UIToolsControl initImageViewWithFrame:CGRectMake(0, ScalePS(260), WIDTH, ScalePS(110)) imageName:@"ball_bg_icon"];
    imgView.userInteractionEnabled = YES;
    [self addSubview:imgView];
    
    sphereView = [[DBSphereView alloc] initWithFrame:CGRectMake(ScalePS(55), ScalePS(40), self.frame.size.width-ScalePS(110), self.frame.size.width-ScalePS(110))];
    sphereView.angle = 0.005;
    [self addSubview:sphereView];
    [self workWithData];
    
    
    UIView *bgView = [UIToolsControl initViewWithFrame:CGRectMake(ScalePS(10), ScalePS(350), WIDTH-ScalePS(20), (idType == TabBarTypeOfPersonal)?ScalePS(70):ScalePS(114)) backgroundColor:COLOR_OF_WHITE];
    [bgView corners:(UIRectCornerTopLeft | UIRectCornerTopRight) radii:CGSizeMake(ScalePS(8), ScalePS(8))];
    bgView.userInteractionEnabled = YES;
    [self addSubview:bgView];
    CGFloat itemWidth = ViewWidth(bgView)/3;
    for (NSInteger i = 0; i < 3; i++) {
        HomeLinkType type = HomeLinkTypeOfCreditHashRate;
        switch (i) {
            case 0: {
                if (idType == TabBarTypeOfPersonal) {
                    type = HomeLinkTypeOfCreditHashRate;
                } else {
                    type = HomeLinkTypeOfJobPromotion;
                }
            } break;
            case 1: {
                type = HomeLinkTypeOfBusinessCard;
            } break;
            case 2: {
                type = HomeLinkTypeOfInvite;
            } break;
            default:
                break;
        }
        HomeLinkItemView *HLIView = [[HomeLinkItemView alloc] initWithFrame:CGRectMake(itemWidth*i+ScalePS(10), 0, itemWidth-ScalePS(10), ScalePS(70)) homeLinkType:type];
        [HLIView addTarget:self action:@selector(homeLinkItemViewTap:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:HLIView];
    }
    
    if (idType == TabBarTypeOfCompany) {
        UIView *bottomView = [UIToolsControl initViewWithFrame:CGRectMake(0, ScalePS(60), WIDTH-ScalePS(20), 44*Scale) backgroundColor:COLOR_OF_WHITE];
        [bgView addSubview:bottomView];
        jobLbl = [UIToolsControl initLabelWithFrame:CGRectMake(ScalePS(10), 0, 300*Scale, 44*Scale) text:nil textColor:THEMECOLOR_OF_TEXT_333333 font:Font_15];
        [bottomView addSubview:jobLbl];
        UILabel *rightLbl = [UIToolsControl initLabelWithFrame:CGRectMake(0, 0, WIDTH-50*Scale, 44*Scale) text:@"已发布的职位 " textColor:THEMECOLOR_OF_TEXT_666666 font:Font_15];
        rightLbl.textAlignment = NSTextAlignmentRight;
        rightLbl.backgroundColor = COLOR_OF_CLEAR;
        [bottomView addSubview:rightLbl];
        UIImageView *imgView = [UIToolsControl initImageViewWithFrame:CGRectMake(EndX(rightLbl)+5*Scale, 15*Scale, 7*Scale, 13*Scale) imageName:@"jiantou_right"];
        imgView.userInteractionEnabled = NO;
        [bottomView addSubview:imgView];
        
        UIButton *selectJobBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, ScalePS(60), WIDTH-ScalePS(20), 44*Scale) imageName:nil target:self action:@selector(selectJobBtnTap) title:nil titleColor:nil];
        [bgView addSubview:selectJobBtn];
    }
}
- (void)reloadDataForBallData:(NSArray *)dataArray {
    if (itemDataArray && itemDataArray.count) {
        [itemDataArray removeAllObjects];
    }
    [itemDataArray addObjectsFromArray:dataArray];
    
    [sphereView removeFromSuperview];
    sphereView = [[DBSphereView alloc] initWithFrame:CGRectMake(ScalePS(55), ScalePS(40), self.frame.size.width-ScalePS(110), self.frame.size.width-ScalePS(110))];
    sphereView.angle = 0.005;
    [self addSubview:sphereView];
    [self workWithData];
}
- (void)workWithData {
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSInteger i = 0; i < itemDataArray.count; i++) {
        PublicCategoryElementModel *model = itemDataArray[i];
        
        UIButton *btn = [UIToolsControl initButtonWithFrame:CGRectMake(0, 0, 60, 40) imageName:nil target:self action:@selector(btnTap:) title:nil titleColor:COLOR_OF_CLEAR];
        btn.tag = BtnTag+i;
        [array addObject:btn];
        [sphereView addSubview:btn];
        
        ZScrollLabel *lbl = [[ZScrollLabel alloc] initWithFrame:CGRectMake(0, (model.canTapFlag)?-5:-3, btn.frame.size.width, btn.frame.size.height*0.5)];
        //        lbl.textColor = THEMECOLOR_OF_ORANGE;
        lbl.textColor = RGB(177, 178, 180);
        lbl.font = Font(Font_11);
        [btn addSubview:lbl];
        
        CGFloat cornerRadius = (model.canTapFlag)?8:4;
        UIView *pointView = [UIToolsControl initViewWithFrame:CGRectMake(0, 0, cornerRadius*2, cornerRadius*2) backgroundColor:RGB(255, 200, 80)];
        pointView.userInteractionEnabled = NO;
        pointView.layer.cornerRadius = cornerRadius;
        pointView.layer.masksToBounds = YES;
        CGPoint vcenter = btn.center;
        pointView.center = vcenter;
        [btn addSubview:pointView];
        
        if (model.canTapFlag) {
            pointView.backgroundColor = RGB(240, 150, 70);
            lbl.font = FontBold(Font_15);
            lbl.textColor = THEMECOLOR_OF_TEXT_333333;
        }
        lbl.text = model.name;
    }
    [sphereView setCloudTags:array];
}
- (void)homeLinkItemViewTap:(HomeLinkItemView *)itemView {
    if (self.delegate && [self.delegate respondsToSelector:@selector(homeBallHeaderViewTap:)]) {
        [self.delegate homeBallHeaderViewTap:itemView.homeLinkType];
    }
}
- (void)btnTap:(UIButton *)btn {
    PublicCategoryElementModel *model = itemDataArray[btn.tag-BtnTag];
    if (!model.canTapFlag) {
        return;
    }
    
    [sphereView timerStop];
    
    [UIView animateWithDuration:0.3 animations:^{
        btn.transform = CGAffineTransformMakeScale(2., 2.);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            btn.transform = CGAffineTransformMakeScale(1., 1.);
        } completion:^(BOOL finished) {
            [self->sphereView timerStart];
        }];
    }];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(homeBallHeaderViewBallPointTap:pointText:)]) {
        [self.delegate homeBallHeaderViewBallPointTap:model.id pointText:model.name];
    }
}
- (void)animationStart {
    if (sphereView) {
        [sphereView timerStart];
    }
}
- (void)animationStop {
    if (sphereView) {
        [sphereView timerStop];
    }
}
- (void)reloadDataForJob:(NSString *)job {
    jobLbl.text = job;
}
- (void)selectJobBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(homeBallHeaderViewSelectJobTap)]) {
        [self.delegate homeBallHeaderViewSelectJobTap];
    }
}

@end
